﻿using System;
using System.Xml;
using CID.Execution.Controller.PackageRegistration;
using NUnit.Framework;

namespace CID.Execution.Controller.Tests.PackageRegistration
{
    [TestFixture]
    class WebPackageRegistrationServiceConfiguratorTests : BaseControllerTests
    {
        private const string ValidConfigurationXml = "<?xml version='1.0' encoding='utf-8' ?><webServiceConfiguration hostingUrl='http://localhost:10001/PackageRegistration'/>";
        private const string InvalidConfigurationXml = "<?xml version='1.0' encoding='utf-8' ?><webServiceConfiguration HostingUrl='http://localhost:10001/PackageRegistration'/>";

        private WebPackageRegistrationServiceConfigurator configurator = null;
        private XmlDocument configurationDocument;

        public override void SetUp()
        {
            base.SetUp();

            configurationDocument = new XmlDocument();
            configurationDocument.LoadXml(ValidConfigurationXml);
            configurator = new WebPackageRegistrationServiceConfigurator(configurationDocument.DocumentElement);
        }

        public override void TearDown()
        {
            if (configurator != null)
                configurator.TearDown();

            base.TearDown();
        }

        [Test]
        public void CreateNew_ShouldSucceed()
        {
            configurator = new WebPackageRegistrationServiceConfigurator(configurationDocument.DocumentElement);

            Assert.IsNotNull(configurator);
            Assert.IsFalse(configurator.IsConfigured);
            Assert.AreEqual(configurationDocument.DocumentElement.OuterXml, configurator.Configuration.OuterXml);
        }

        [Test]
        public void TestConfigure_ValidConfiguration_ShouldSucceed()
        {
            configurator.SetUp();

            Assert.IsTrue(configurator.IsConfigured);
        }

        [Test, ExpectedException(typeof(UriFormatException))]
        public void TestConfigure_InvalidConfiguration_ShouldThrowException()
        {
            configurationDocument = new XmlDocument();
            configurationDocument.LoadXml(InvalidConfigurationXml);
            configurator = new WebPackageRegistrationServiceConfigurator(configurationDocument.DocumentElement);

            configurator.SetUp();
        }
    }
}
